package controller

import (
	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/gin-gonic/gin"
	"net/http"
	"shendaNFT/pkg/web/util"
)

type GenerateWalletResult struct {
	Key     string `json:"key"`
	Address string `json:"address"`
}

// GenerateWallet 获取随机生成的钱包地址
func (c *Controller) GenerateWallet(ctx *gin.Context) {
	privateKey, err := crypto.GenerateKey()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, util.ErrorResponse(err.Error()))
	}
	address := crypto.PubkeyToAddress(privateKey.PublicKey).String()
	privateKeyBytes := crypto.FromECDSA(privateKey)
	key := hexutil.Encode(privateKeyBytes)[2:]

	ctx.JSON(http.StatusOK, util.SuccessResponse(GenerateWalletResult{
		Address: address,
		Key:     key,
	}))
}
